package cn.action;

import java.io.IOException;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import cn.service.CommonService;
import cn.service.DeleteOrUpdateBySQLService;
import cn.service.FindSomeByHQLService;

import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport  {


	//编码
	public final static String CODE_UTF8 = "UTF-8";//utf-8编码
	public final static String CODE_GBK = "gbk";//gbk编码
	public String jsonStr ; //json格式字符串，用于页传值
	// 返回参数
	protected static final String GLOBAL_MAIN = "globalMain";
	protected static final String GLOBAL_OUT = "globalOut";
	protected static final String OK = "ok";
	protected static final String EDIT = "edit";
	protected static final String ERR = "err";
	protected static final String INPUT = "input";
	protected static final String MESSAGE = "message";
	protected static final String SUCCESS = "success";
	protected static final String NULL_VAL = "null";
	protected static final String DUL = "dul";
	// 获得servlet API
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected ServletContext application;
	
	//判断是否主图片 
	protected static final String mainPic_0 = "0";//不是主图片
	protected static final String mainPic_1 = "1";//是主图片

	/**
	 * 把json格式数据转换为json对象
	 * @param jsonStr
	 * @return
	 */
	public JSONObject changeToJsonObj(String jsonStr){
		jsonStr  = this.jsonStr;
		JSONObject jsonObj = new JSONObject();
		try {
			jsonObj = JSONObject.fromObject(java.net.URLEncoder.encode(jsonStr,CODE_UTF8));
			return jsonObj;
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return jsonObj;
		}
	}

	/**
	 * 把list转换为json数组
	 * @param list list对象
	 * @return
	 */
	public String changeListToStr(List list){
		if(list.equals(null)||null ==  list){
			return null;
		}else{
			JSONArray json = JSONArray.fromObject(list);
			return json.toString();
		}
	}


	/**
	 * 把一个pojo转换为一个json字符串
	 * @param <T> 泛型
	 * @param t
	 * @return
	 */
	public <T extends Serializable> String changeBeanToStr(T t){
		if(t.equals(null)||null == t){
			return null;
		}else{
			JSONArray json = JSONArray.fromObject(t);
			return json.toString();
		}
	}

	/**
	 * 把一个map转为json字符串，map内对象可以是字符串也可以一个个bean
	 * @param m 一个map
	 * @return
	 */
	public String changeMapToStr(Map m){
		if(m.equals(null)||null == m){
			return null;
		}else{
			JSONArray json = JSONArray.fromObject(m);
			return json.toString();
		}
	}

	/**
	 * 此方法若前台使用js的serialize方法，可以直接在后台获取，不用经过json
	 * 把一个以bean格式封装的str,如：
	 * {"spotegs":{"id":"id","areaMsg":"areaMsg","describe":"describe"}
	 * 转换为一个真正的bean
	 * @param str json字符串
	 * @param beanName 例子中的spotegs
	 * @param clazz 需要转换成的dojo的class
	 * @return
	 */

	public Object changeStrToBean(String str,String beanName,Class clazz){
		//JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(dateFormats));
		if("".equals(str)||str.equals(null)||null==str){
			return null;
		}else{
			JSONObject jsonObj = JSONObject.fromObject(str);
			Object obj = JSONObject.toBean(JSONObject.fromObject(jsonObj.get(beanName)),clazz);
			return 	 obj;
		}
	}



	/**
	 * 使用ajax发送text数据
	 *
	 * @param content
	 * @throws IOException
	 */
	public void sendMsgAjax(String content,String code) throws IOException {
		HttpServletResponse response = ServletActionContext.getResponse();
		if(code==null){
			response.setCharacterEncoding(CODE_UTF8);
		}else{
			response.setCharacterEncoding(""+code+"");
		}
		response.getWriter().write(content);
		response.setContentType("text/html");
		response.getWriter().close();
	}


	/**
	 * 获得请求IP地址.
	 *
	 * @return
	 */
	public String getIpAddr() {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

	/**
	 * 属性定义
	 */

	public CommonService commonservice;

	public DeleteOrUpdateBySQLService deleteOrUpdateBySQL;

	public FindSomeByHQLService findSomeByHQL;




	/**
	 * get() set()方法
	 */

	public CommonService getCommonservice() {
		return commonservice;
	}

	public void setCommonservice(CommonService commonservice) {
		this.commonservice = commonservice;
	}

	public DeleteOrUpdateBySQLService getDeleteOrUpdateBySQL() {
		return deleteOrUpdateBySQL;
	}

	public void setDeleteOrUpdateBySQL(
			DeleteOrUpdateBySQLService deleteOrUpdateBySQL) {
		this.deleteOrUpdateBySQL = deleteOrUpdateBySQL;
	}

	public FindSomeByHQLService getFindSomeByHQL() {
		return findSomeByHQL;
	}

	public void setFindSomeByHQL(FindSomeByHQLService findSomeByHQL) {
		this.findSomeByHQL = findSomeByHQL;
	}

}
